﻿//***********************************************************************
//* Module:  AppOrganizatiOnUserHandler.ashx
//* Author:  XWG
//* Modified: 2013-2-19
//* Purpose: 为前台页面提供请求处理页面--AppOrganizatiOnUserHandler类
//***********************************************************************/
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.Framework.AuthBLL;
using JWBST.CommonClass;
using XWG.JWBST.JWBSTSPConst;
using XWG.Framework;
using JWBSTAPP.CommonClass;
using XWG.Framework.AuthUtility;
namespace JWBSTAPP.AjaxServer.User
{
    /// <summary>
    /// AppOrganizatiOnUserHandler 的摘要说明
    /// </summary>
    public class AppUserHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel();//初始化返回类型
            string paramKey = context.Request.Params["id"];
            if (string.IsNullOrEmpty(paramKey))
            {
                paramKey = Convert.ToString(context.Session["UserID"]);
            }
            string json = "";
            try
            {
                AppOrganizatiOnUser appOrganizatiOnUser = new AppOrganizatiOnUser();//声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    appOrganizatiOnUser = XWG.Framework.AuthBLL.AppOrganizatiOnUserBLL.Load(paramKey);//根据键值获取管理员
                    if (appOrganizatiOnUser != null)
                    {
                        json = JsonConvert.SerializeObject(appOrganizatiOnUser);
                        Hashtable hashtable = new Hashtable();
                        if (!String.IsNullOrEmpty(appOrganizatiOnUser.OrgnaId))
                        {
                            EntOrganizatiOn parent = EntOrganizatiOnBLL.Load(appOrganizatiOnUser.OrgnaId);
                            if (parent != null)
                                hashtable.Add("OrgnaName", parent.OrgnaName);
                        }
                        if (!String.IsNullOrEmpty(appOrganizatiOnUser.AreaId))
                        {

                            AppAreA appAreA = AppAreABLL.Load(appOrganizatiOnUser.AreaId);
                            if (appAreA != null)
                                hashtable.Add("AreaName", appAreA.AreaName);
                        }
                        if (!String.IsNullOrEmpty(appOrganizatiOnUser.Enabled))
                        {
                            string userSex = "";
                            if (appOrganizatiOnUser.UserSex == "0")
                                userSex = "男";
                            else if (appOrganizatiOnUser.UserSex == "1")
                                userSex = "女";
                            hashtable.Add("UserSexName", userSex);
                        }
                        if (!String.IsNullOrEmpty(appOrganizatiOnUser.Enabled))
                        {
                            string enabledName = "";
                            if (appOrganizatiOnUser.Enabled == "0")
                                enabledName = "有效";
                            else if (appOrganizatiOnUser.Enabled == "1")
                                enabledName = "无效";
                            hashtable.Add("EnabledName", enabledName);
                        }
                        json = JsonString.AddAttribute(json, hashtable);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }

        protected void GetModelBySessionID()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel();//初始化返回类型
            string paramKey = context.Request.Params["id"];
            if (string.IsNullOrEmpty(paramKey))
            {
                paramKey = Convert.ToString(context.Session["UserID"]);
            }

            string json = "";
            try
            {
                AppOrganizatiOnUser appOrganizatiOnUser = new AppOrganizatiOnUser();//声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    appOrganizatiOnUser = XWG.Framework.AuthBLL.AppOrganizatiOnUserBLL.Load(paramKey);//根据键值获取管理员
                    if (appOrganizatiOnUser != null)
                    {
                        json = JsonConvert.SerializeObject(appOrganizatiOnUser);
                        Hashtable hashtable = new Hashtable();
                        if (!String.IsNullOrEmpty(appOrganizatiOnUser.OrgnaId))
                        {
                            AppOrganizatiOn mO = new AppOrganizatiOn();
                            EntOrganizatiOn parent = EntOrganizatiOnBLL.Load(appOrganizatiOnUser.OrgnaId);
                            if (parent != null)
                                hashtable.Add("OrgnaName", parent.OrgnaName);
                        }
                        if (!String.IsNullOrEmpty(appOrganizatiOnUser.AreaId))
                        {

                            AppAreA appAreA = AppAreABLL.Load(appOrganizatiOnUser.AreaId);
                            if (appAreA != null)
                                hashtable.Add("AreaName", appAreA.AreaName);
                        }
                        if (!String.IsNullOrEmpty(appOrganizatiOnUser.Enabled))
                        {
                            string userSex = "";
                            if (appOrganizatiOnUser.UserSex == "0")
                                userSex = "男";
                            else if (appOrganizatiOnUser.UserSex == "1")
                                userSex = "女";
                            hashtable.Add("UserSexName", userSex);
                        }
                        if (!String.IsNullOrEmpty(appOrganizatiOnUser.Enabled))
                        {
                            string enabledName = "";
                            if (appOrganizatiOnUser.Enabled == "0")
                                enabledName = "有效";
                            else if (appOrganizatiOnUser.Enabled == "1")
                                enabledName = "无效";
                            hashtable.Add("EnabledName", enabledName);
                        }

                        hashtable.Add("LoginTime", DateTime.Now.ToString());

                        json = JsonString.AddAttribute(json, hashtable);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        /// <summary>
        /// PWDEdit
        /// </summary>
        protected void PWDEdit()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Session["UserID"].ToString();
            string paramoldPWD = context.Request.Params["texPasswordOld"];
            string paramnewPWD = context.Request.Params["texPasswordNew"];
            string json = "";
            try
            {
                AppOrganizatiOnUser appOrganizatiOnUser = new AppOrganizatiOnUser();//声明实体
                if (!string.IsNullOrEmpty(paramnewPWD))
                {
                    //取用户密码
                    var user = AppOrganizatiOnUserBLL.Load(paramKey);
                    if (user != null)
                    {
                        if (user.UserPassword == paramoldPWD)
                        {
                            mResultModel = AppOrganizatiOnUserBLL.PWDEdit(Convert.ToString(context.Session["UserID"]), paramoldPWD, paramnewPWD, true);
                            json = JsonConvert.SerializeObject(mResultModel);
                            mResultModel.ISSucceed = true;
                        }
                        else
                        {
                            mResultModel.ErrorCode = -9993;
                            mResultModel.ISSucceed = false;
                            mResultModel.Message = "原密码输入错误";
                            json = JsonConvert.SerializeObject(mResultModel);
                        }
                    }
                    else
                    {
                        mResultModel.ErrorCode = -1;
                        mResultModel.ISSucceed = false;
                        mResultModel.Message = "修改失败";
                        json = JsonConvert.SerializeObject(mResultModel);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -1;
                mResultModel.ISSucceed = false;
                mResultModel.Message = "修改失败";
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }

        /// <summary>
        /// 获取组织机构下拉
        /// </summary>
        protected void GetORGList()
        {
            List<ControlID> selResPid = new List<ControlID>();
            //selEnabled
            ControlID CID = new ControlID();
            //ControlList controllist = new ControlList();

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";

            DataTable dtORG = new DataTable();

            QueryByPage Q = new QueryByPage();

            DataTable dt = new DataTable();


            dtORG = Q.GetRecordByPage("*", "", "", 0, 1, AdminResouRceConst.TableName, AdminResouRceConst.FieldKey, "").Tables[0];
            XWG.Framework.AuthUtility.AnyString.MakeTree(dtORG, AdminResouRceConst.Columns.ResPid, "0", AdminResouRceConst.FieldKey, AdminResouRceConst.Columns.ResName, ref dt, -1);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    CID = new ControlID();
                    CID.name = Convert.ToString(dt.Rows[i][AdminResouRceConst.Columns.ResName]);
                    CID.value = Convert.ToString(dt.Rows[i][AdminResouRceConst.Columns.ResId]);
                    selResPid.Add(CID);
                }
            }
            string json = JsonConvert.SerializeObject(selResPid);
            context.Response.Write(json);
        }
    }
}


